# host_management/serializers.py
from rest_framework import serializers
from .models import City, IDC, Host, HostStat


class CitySerializer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields = '__all__'


class IDCSerializer(serializers.ModelSerializer):
    city_name = serializers.CharField(source='city.name', read_only=True)

    class Meta:
        model = IDC
        fields = '__all__'
        extra_fields = ['city_name']


class HostSerializer(serializers.ModelSerializer):
    idc_name = serializers.CharField(source='idc.name', read_only=True)
    city_name = serializers.CharField(source='idc.city.name', read_only=True)

    class Meta:
        model = Host
        fields = '__all__'
        extra_fields = ['idc_name', 'city_name']
        read_only_fields = ['last_password_change']

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        representation.pop('root_password', None)
        return representation


class HostStatSerializer(serializers.ModelSerializer):
    city_name = serializers.CharField(source='city.name')
    idc_name = serializers.CharField(source='idc.name')

    class Meta:
        model = HostStat
        fields = '__all__'